package com.umedcn.controller;

import com.umedcn.dao.UserDao;
import com.umedcn.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangadam on 2017/7/18.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("/getAll")
    @ResponseBody
    public List<User> getAllUsers(){
        List<User> data = userDao.getAllUsers();
        return data;
    }

    @RequestMapping("/index")
    public String index(ModelMap modelMap){
        User user = userDao.findUserById(1);
        List<User> users = userDao.getAllUsers();
        modelMap.put("users", users);
        System.out.println(users + "-----------");
        return "index";
    }

    @RequestMapping("/users")
    public Map<String, Object> getUsers(HttpServletRequest request){
        String user_id = request.getParameter("user_id");

        User user = userDao.getUsers(1);

        Map<String, Object> data = new HashMap<>();
        data.put("user", user);

        System.out.println(user.getPosts());

        return data;
    }
}
